/*
 * Cubo.cpp
 *
 *  Created on: Oct 13, 2012
 *      Author: romina
 */

#include "Cubo.h"

Cubo::Cubo() {
	this->td = 1;
	this->id = 0;
	this->tamanio = sizeof(int) * 4;

}

int Cubo::get_id(){
	return this->id;
}

void Cubo::set_id(int id){
	this->id=id;
}
bool Cubo::overflow(int cant){
	return this->tamanio + cant > TAM_CUBO;
}

int Cubo::get_td(){
	return this->td;
}

void Cubo::set_td(int td){
	this->td = td;
}

void Cubo::duplicar_td(){
	this->td *= 2;
}

void Cubo::truncar_td(){
	this->td /= 2;
}


void Cubo::insertar_registro(RecordFrase record){

	this->registros.push_back(record);
	this->tamanio += record.getTamanio();

}


void Cubo::buscar_registro(int id, RecordFrase & registro){
	bool encontrado = false;
	list<RecordFrase>::iterator itrecord;
	for(itrecord = this->registros.begin(); itrecord != this->registros.end(); ++itrecord){
		if ((*itrecord).getId() == id){
			registro = (*itrecord);
			encontrado = true;
			break;
		}
	}
	if (!encontrado)
		throw RecordNotFoundException("No se encontro la frase");
}

void Cubo::modificar_registro(RecordFrase record){
	bool encontrado = false;
	list<RecordFrase>::iterator itrecord;
	for(itrecord = this->registros.begin(); itrecord != this->registros.end(); ++itrecord){
		if ((*itrecord).getId() == record.getId()){
			this->tamanio -= (*itrecord).getTamanio();
			this->tamanio += record.getTamanio();
			this->registros.erase(itrecord);
			this->registros.push_back(record);
			encontrado = true;
			break;
		}
	}
	if (!encontrado)
		throw RecordNotFoundException("No se encontro la frase");
}


void Cubo::eliminar_registro(int id){
	bool encontrado = false;
	list<RecordFrase>::iterator itrecord;
		for(itrecord = this->registros.begin(); itrecord != this->registros.end(); ++itrecord){
			if ((*itrecord).getId() == id){
				this->tamanio -= (*itrecord).getTamanio();
				this->registros.erase(itrecord);
				encontrado = true;
				break;
			}
		}

	if(!encontrado)
		throw RecordNotFoundException("No se encontro la frase");
}

list<RecordFrase> Cubo::listar_registros(){
	return this->registros;
}

int Cubo::cantidad_reg(){
	return this->registros.size();
}

void Cubo::serializar(char * buffer, unsigned int &offset){

	PersistorHASH::transformar_entero_en_char(buffer, offset, this->id);
	PersistorHASH::transformar_entero_en_char(buffer, offset, this->tamanio);
	PersistorHASH::transformar_entero_en_char(buffer, offset, this->td);
	PersistorHASH::transformar_entero_en_char(buffer, offset, this->cantidad_reg());
	list<RecordFrase>::iterator itrecord;
	for(itrecord = this->registros.begin(); itrecord != this->registros.end(); ++itrecord){
		(*itrecord).serializar(buffer, offset);
	}

}

void Cubo::deserializar(char * buffer, unsigned int &offset){
	this->registros.clear();
	this->id = PersistorHASH::obtener_entero_desde_Buffer(buffer, offset);
	this->tamanio = PersistorHASH::obtener_entero_desde_Buffer(buffer, offset);
	this->td = PersistorHASH::obtener_entero_desde_Buffer(buffer, offset);
	int cant_reg = PersistorHASH::obtener_entero_desde_Buffer(buffer, offset);
	for(int i = 0; i < cant_reg; i++){
		RecordFrase record;
		record.deserializar(buffer, offset);
		this->registros.push_back(record);
	}
}

string Cubo::toString(){
	string cubo = "\n";
	cubo.append("ID: " + Palabra::intToString(this->id) + " ");
	cubo.append("TAMANIO: " + Palabra::intToString(this->tamanio) + " ");
	cubo.append("cantREG: "+ Palabra::intToString(this->cantidad_reg()) + " ");
	cubo.append("DISPERSION: " + Palabra::intToString(this->td) + "\n");
	list<RecordFrase>::iterator itrecord;
	for(itrecord = this->registros.begin(); itrecord != this->registros.end(); ++itrecord){
			string s = (*itrecord).toString() + '\n';
			cubo.append(s);
	}
	return cubo;
}

string Cubo::toStringInfo(){
	int reg = 0;
	string cubo = "";
	cubo.append("ID: " + Palabra::intToString(this->id) + " | ");
	cubo.append("TAMANIO: " + Palabra::intToString(this->tamanio) + " | ");
	list<RecordFrase>::iterator itrecord;
		for(itrecord = this->registros.begin(); itrecord != this->registros.end(); ++itrecord){
				reg++;
		}
	cubo.append("CANT REG: "+ Palabra::intToString(this->cantidad_reg()) + " = " + Palabra::intToString(reg) + " | ");
	cubo.append("DISPERSION: " + Palabra::intToString(this->td) + " ");
	return cubo;
}

void Cubo::show(){
	cout << "ID: " << this->id << " ";
	cout << "TAMANIO: " << this->tamanio << " ";
	cout << "cantReg: " << this->cantidad_reg() << " ";
	cout << "DISPERSION: " << this->td << endl;
	list<RecordFrase>::iterator itrecord;
		for(itrecord = this->registros.begin(); itrecord != this->registros.end(); ++itrecord){
				string s = (*itrecord).toString();
				cout << s << endl;
		}
	cout << endl;
}

Cubo::~Cubo() {
}

